package com.company.HDFS;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

import java.io.IOException;

/**
 * @author ChinaManor
 * #Description hbase的javaAPI
 * #Date: 2021/12/19 18:10
 */
public class step2 {
    /**
     * @Description: createTable():创建表的方法
     * @Param: 0
     * @return: 0
     */

    @Test
    public void createTable() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "node1:2181");
        //建立连接
        Connection conn = ConnectionFactory.createConnection(conf);
        //获取表的管理类
        Admin admin = conn.getAdmin();
        //定义表
        HTableDescriptor hTableDescriptor=new HTableDescriptor(TableName.valueOf("demo"));
        //定义列簇
        HColumnDescriptor hColumnDescriptor =new HColumnDescriptor("info");
        //讲列簇定义到表中
        hTableDescriptor.addFamily(hColumnDescriptor);
        //执行建表操作
        admin.createTable(hTableDescriptor);
        admin.close();
        conn.close();


    }

    /**
     * @Description: 向Hbase中插入数据的方法
     * @Param: null
     * @return: null
     */

    @Test
    public void put(){
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node1:2181");

        try {
            //建立连接
            Connection conn= ConnectionFactory.createConnection(conf);
            //获取表
            Table table=conn.getTable(TableName.valueOf("demo"));
            //用行键实例化put
            Put put= new Put("rk001".getBytes());
            //指定列簇名，列名，和值
            put.addColumn("info".getBytes(),"name".getBytes(),"zhangsan".getBytes());
            table.put(put);
            table.close();
            conn.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Description: scan()查询一个表的所有信息
     * @Param: 1
     * @return: 1
     */

    @Test
    public  void scan() throws IOException {
        Configuration conf=HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "node1:2181");

        //建立连接
        Connection conn=ConnectionFactory.createConnection(conf);

        //获取表
        Table table=conn.getTable(TableName.valueOf("demo"));


        //初始化Scan实例
        Scan scan=new Scan();

        //增加过滤条件
        scan.addColumn("info".getBytes(), "name".getBytes());
        //返回结果
        ResultScanner rss=table.getScanner(scan);
        //迭代并取出结果
        for(Result rs:rss){
            String valStr=Bytes.toString(rs.getValue("info".getBytes(), "name".getBytes()));
            System.out.println(valStr);
        }


        //关闭连接
        table.close();
        conn.close();

    }

    /**
     * @Description: delete()删除表中的信息
     * @Param: 1
     * @return: 1
     */

    @Test
    public  void delete() throws IOException {
        Configuration conf=HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "node1:2181");

        //建立连接
        Connection conn=ConnectionFactory.createConnection(conf);

        //获取表
        Table table=conn.getTable(TableName.valueOf("demo"));


        // 用行键来实例化Delete实例
        Delete del = new Delete("rk0001".getBytes());
        // 执行删除
        table.delete(del);


        //关闭连接
        table.close();
        conn.close();
    }
}
